﻿function Widget_Weather(obj)
{
   this.initializeBase(obj);
}
Widget_Weather.prototype = new WidgetInstance;

Widget_Weather.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.Widget.obj.State);
      this.Refresh();
   }
}

Widget_Weather.prototype.Edit = function()
{
   this.Widget.InstanceEdit();
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      if(this.inEditMode)
         this.Settings_TxtCountryCode.focus();
   }   
}

Widget_Weather.prototype.Refresh = function()
{
   WriteHistory("Widget_Weather.prototype.Refresh #" + this.id);
   //Query proxyasync for the rss feed
   
   this.FeedURL = "http://weather.yahooapis.com/forecastrss?p=" + this.Widget.obj.State;
   
   Proxy.GetRssFeed(this.FeedURL,1,10,false,Function.createDelegate( this, this.onContentLoad ));
}

Widget_Weather.prototype.onContentLoad = function(rss)
{
   WriteHistory("Widget_Weather.prototype.onContentLoad #" + this.id);
     var div = this.WidgetDiv_BodyContent;
     div.innerHTML = "";
     
     if (!rss)
     {
        div.innerHTML = "we are unable to grab the weather details";
        return;
     }
     else if (rss.length < 1)
     {
      div.innerHTML = "No weather information was returned";
     }

     if (rss.length > 0)
     {
         var item = rss[0];
         
         //First display as text
         var span = document.createElement("span");
         
         var content = item.Title + "<br>" + (item.ContentEncoded.length < 1 ? "<span class='feed_item_link'>" + item.Description + "</span>": item.ContentEncoded);

         var x = "";
         
         //patch for yahoo weather, the link opens in same window
         var idx = content.indexOf("<a ");
         if (idx > 0)
         {
            content = content.substring(0, idx+3) + " target='_blank' " + content.substring(idx+3);
         }
         span.innerHTML = content;
         
         div.appendChild(span);
      }
}

Widget_Weather.prototype.Load = function()
{
   this.Menu_Edit = new Array();
   this.Menu_Edit.push("m" + this.id + "_edit");
   this.Widget.InstanceLoad();
      
   this.WidgetDiv_BodySettings = $get('wdivbs' + this.id);
   this.WidgetDiv_BodyContent = document.getElementById("wdivbc" + this.Widget.id);
   this.WidgetDiv_Command_Edit = document.getElementById("wdivc_edit" + this.Widget.id);
   
   this.Settings_TxtCountryCode = $get('TxtCountryCode' + this.id);
   this.Settings_TxtCountryCode.value = this.Widget.obj.State;
   this.Refresh();  
   setDefaultButton(this.Settings_TxtCountryCode.id, "btn_Save" + this.id);
   
   var img = $('wimg' + this.id);
   img.src = img.src;
   this.Widget.InstanceLoadComplete();
}

Widget_Weather.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      if (this.Settings_TxtCountryCode.value.length > 0)
      {
         this.Widget.obj.State = this.Settings_TxtCountryCode.value;

         JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.Widget.obj.State);
         
         this.inEditMode = true;
         this.Edit();
         
         this.Refresh();
      }
      else
      {
         alert("Country code is a required field");
         this.Settings_TxtCountryCode.focus();
      }
   }
}